package main

import "sync"

// ConvertHandlerQueue 任务待处理队列
type ConvertHandlerQueue struct {
	TasksQueue []interface{}
	QueueLock  sync.Mutex
}

// InitQueue 初始化队列
func InitQueue() *ConvertHandlerQueue {
	return &ConvertHandlerQueue{
		TasksQueue: make([]interface{}, 0),
	}
}

// Enqueue 入列
func (q *ConvertHandlerQueue) Enqueue(val interface{}) {
	q.QueueLock.Lock()
	defer q.QueueLock.Unlock()

	q.TasksQueue = append(q.TasksQueue, val)
}

// Dequeue 出列
func (q *ConvertHandlerQueue) Dequeue() (val interface{}, ok bool) {
	q.QueueLock.Lock()
	defer q.QueueLock.Unlock()

	if len(q.TasksQueue) == 0 {
		return nil, false
	}

	val = q.TasksQueue[0]
	q.TasksQueue = q.TasksQueue[1:]
	return val, true
}

// QueueIsEmpty 判空
func (q *ConvertHandlerQueue) QueueIsEmpty() bool {
	q.QueueLock.Lock()
	defer q.QueueLock.Unlock()

	return len(q.TasksQueue) == 0
}
